<template>
  <div class="container">
    <el-card>
      <el-form label-width="80px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input v-model="input" placeholder="根据编号搜索"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="12">
            <el-form-item style="text-align: right">
              <el-button @click="clickDel">清除</el-button>
              <el-button @click="clickSearch" type="primary">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-alert type="info" show-icon>
        <template #title>
          {{ `数据共${counts}条` }}
        </template>
      </el-alert>
      <el-table :data="list">
        <el-table-column prop="id" label="编号"></el-table-column>
        <el-table-column
          prop="questionType"
          :formatter="type"
          label="题型"
        ></el-table-column>
        <el-table-column prop="questionIDs" label="题目编号">
          <template #default="{ row }">
            <el-link
              @click.prevent="handleLook(item.id)"
              v-for="(item, index) in row.questionIDs"
              :key="index"
            >
              {{ item.number }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column prop="addTime" label="录入时间"></el-table-column>
        <el-table-column
          prop="totalSeconds"
          label="答题时间(s)"
        ></el-table-column>
        <el-table-column
          prop="accuracyRate"
          label="正确率(%)"
        ></el-table-column>
        <el-table-column prop="userName" label="录入人"></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              @click="handleDel(row.id)"
              type="danger"
              icon="el-icon-delete"
              circle
              plain
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: right"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[20, 30, 50, 100]"
        :page-size="pagesize"
        layout="total,  prev, pager, next,sizes, jumper"
        :total="counts"
      >
      </el-pagination>
      <el-dialog title="题目预览" :visible="isShow" @close="handleClose">
        <el-row>
          <el-col :span="6">【题型】：{{  tx }}</el-col>
          <el-col :span="6">【编号】：{{ obj.id }}</el-col>
          <el-col :span="6">【难度】：{{ nd  }}</el-col>
          <el-col :span="6">【标签】：{{ obj.tags }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="6">【学科】：{{ obj.subjectName }}</el-col>
          <el-col :span="6">【目录】：{{ obj.directoryName }}</el-col>
          <el-col :span="6">【方向】：{{ obj.direction }}</el-col>
          <el-col :span="6"></el-col>
        </el-row>
        <hr />
        【答案解析】：
        <div style="color: skyblue" v-html="obj.question"></div>
        <el-row>{{tx}}选项:(以下选中的选项为正确答案)</el-row>
        <div v-for="item in obj.options" :key="item.code">
        <el-radio v-if="obj.questionType==='1'" :value="item.isRight" :label="1">{{item.title}}</el-radio>
        <el-checkbox v-if="obj.questionType==='2'" :value="item.isRight?true:false">{{item.title}}</el-checkbox>
        </div>
        <hr />
        <el-row>
          <span>【参考答案】：</span>&nbsp;
          <el-button type="danger" @click="videoPreview">视频答案预览</el-button>
          <div class="video" v-show="play">
            <video ref="video" :src="obj.videoURL" controls></video>
          </div>
        </el-row>
        <hr />
        【答案解析】：
        <div style="display: inline-block" v-html="obj.answer"></div>
        <hr />
        <el-row>【题目备注】：{{ obj.remarks }}</el-row>
        <template #footer>
          <div style="text-align: right">
            <el-button @click="handleClose" type="primary">关闭</el-button>
          </div>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions'
import { questionType, difficulty } from '@/api/hmmm/constants'
export default {
  data () {
    return {
      list: [],
      page: 1,
      pagesize: 20,
      counts: 0,
      input: '',
      isShow: false,
      isNext: true,
      obj: {},
      play: false
      // keyword: ''
    }
  },
  computed: {
    tx () {
      const obj = questionType.find((item) => item.value === +this.obj.questionType)
      return obj ? obj.label + '题' : '未知'
    },
    nd () {
      const obj1 = difficulty.find(item => item.value === +this.obj.difficulty)
      return obj1 ? obj1.label : '未知'
    }
  },
  created () {
    this.randomsList()
  },
  methods: {
    async randomsList () {
      const { data } = await randoms({
        page: this.page,
        pagesize: this.pagesize,
        keyword: this.input
      })
      // console.log(data)
      this.list = data.items
      // console.log(this.list)
      this.counts = data.counts
    },
    type (row, column, cellValue, index) {
      const obj = questionType.find((item) => item.value === +cellValue)
      return obj ? obj.label : '未知'
    },
    handleSizeChange (val) {
      this.pagesize = val
      this.page = 1
      this.randomsList()
    },
    handleCurrentChange (val) {
      this.page = val
      this.randomsList()
    },
    clickDel () {
      this.input = ''
    },
    async clickSearch () {
      this.page = 1

      this.randomsList()
    },
    async handleDel (id) {
      // console.log(id)

      try {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      } catch (e) {
        console.log('取消')
      }
      await removeRandoms(JSON.stringify(id))

      this.randomsList()
    },
    async handleLook (id) {
      console.log(id)
      const { data } = await detail({ id: id, isNext: this.isNext })
      // console.log(data)
      this.obj = data
      console.log(this.obj)
      this.isShow = true
    },
    handleClose () {
      this.isShow = false
      this.play = false
    },
    videoPreview () {
      this.play = true
    }
  }
}
</script>

<style scoped lang="scss">
.container {
  margin: 10px 15px 0px 10px;
}
.el-table {
  margin: 10px;
}
.el-col {
  height: 36px;
}
//
</style>
